package 正则;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PatternDemo {
    public static void main(String[] args) {
        String age = "123";
        int parseAge = Integer.parseInt(age);
        String rexAge = "^[0-9]{1,3}$";
        Pattern pAge =  Pattern.compile(rexAge);
        Matcher m = pAge.matcher(age);
        System.out.println(m.find());
        while(m.find()){
            System.out.println( "start : " +m.start());
            System.out.println("end :" +m.end());
            System.out.println( "group :" +m.group());
        }

        System.out.println("===============================");
        boolean ret =new PatternDemo().isMatch("aa","a*");
        System.out.println(ret);
    }

    public boolean isMatch(String s, String p) {
        int m = s.length(), n = p.length();
        boolean[][] dp = new boolean[m + 1][n + 1];
        // base case
        // dp[i][0] = false;
        dp[0][0] = true;
        for (int j = 1; j <= n; j++) {
            //dp[0][j]有可能为true，即有x*的时候可以省略
            if (p.charAt(j - 1) == '*')
                dp[0][j] = dp[0][j - 2];
        }

        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= n; j++) {
                char si = s.charAt(i - 1), pj = p.charAt(j - 1);
                if ('a' <= pj && pj <= 'z'){
                    if (si == pj)
                        dp[i][j] = dp[i - 1][j - 1];
                } else if (pj == '.'){
                    dp[i][j] = dp[i - 1][j - 1];
                } else {
                    char pre_pj = p.charAt(j - 2);
                    if (si != pre_pj && pre_pj != '.'){
                        dp[i][j] = dp[i][j - 2];
                    } else {
                        //这里要加上dp[i][j - 2]是针对于p[j]前一个是'.'的情况
                        //如：s = “af”，p = "af.*"，dp[2][4]应该是true，所以此时dp[2][4] = dp[2][2];
                        //如：s = “afs”，p = "af.*"，dp[3][4]也是true，但是此时dp[3][4] = dp[2][4];
                        dp[i][j] = dp[i - 1][j] || dp[i][j - 2];
                    }
                }
            }
        }
        return dp[m][n];
    }

    class Solution {
        public boolean isMatch(String s, String p) {
            int slength = s.length();
            int plength = p.length();
            boolean[][] dp = new boolean[slength+1][plength+1];
            dp[0][0] = true;
            // p 中连续含有两个**的
            for(int j=1; j < plength; j++){
                if(p.charAt(j-1) == '*'){
                    dp[0][j] = dp[0][j-2];
                }
            }
            //
            for(int i = 1; i <= slength; i++){
                for(int j = 1; j <= plength; j++){
                    //p中没有 . 和 *
                    if(p.charAt(j) != '.' && p.charAt(j) != '*'){
                        if(s.charAt(i-1) == p.charAt(j-1)){
                            dp[i][j] = dp[i-1][j-1];
                        }
                    }else if(p.charAt(j-1) == '.'){
                        dp[i][j] = dp[i-1][j-1];
                    }else{  //带有 *的
                        char pre_pj = p.charAt(j - 2);
                        if (s.charAt(i-1) != p.charAt(j-2) && p.charAt(j-2) != '.'){
                            dp[i][j] = dp[i][j - 2];
                        }else{
                            /**
                             *  for example :
                             *          1： s = "ab"; p = "ab.*" 。 ".*" 可以省略 , dp[2][4]应该是true，所以此时dp[2][4] = dp[2][2]
                             *          2： s = "abs"; p= "ab.*" 。 ".*" 可以代表s, dp[3][4]应该是true,所以此时dp[3][4] = dp[2][4]
                             */
                            dp[i][j] = dp[i-1][j] || dp[i][j-2];
                        }
                    }
                }
            }
            return dp[slength][plength];
        }
    }

}
